#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Contains the objects required for RATS traffic resource access. """

try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

import ratom
import rawsc
from rawsc import RAWS_NAMESPACE
from rawsc.rams import *

class TrafficParams(rawsc.RawsParams):
    """ Handler for the <raws:params> element (= sub-element of atom:content). """
    _children = {}
    _children['{%s}hits' % rawsc.RAWS_NAMESPACE] = ('hits', Hits)
    _children['{%s}volume' % RAWS_NAMESPACE] = ('volume', Volume)
    _children['{%s}path' % RAWS_NAMESPACE] = ('path', Path)
    _children['{%s}updated' % rawsc.RAWS_NAMESPACE] = ('updated', Updated)
    _children['{%s}last_request' % RAWS_NAMESPACE] = ('last_request', LastRequest)
    _children['{%s}pct_0_10' % RAWS_NAMESPACE] = ('pct_0_10', Pct010)
    _children['{%s}pct_10_20' % RAWS_NAMESPACE] = ('pct_10_20', Pct1020)
    _children['{%s}pct_20_30' % RAWS_NAMESPACE] = ('pct_20_30', Pct2030)
    _children['{%s}pct_30_40' % RAWS_NAMESPACE] = ('pct_30_40', Pct3040)
    _children['{%s}pct_40_50' % RAWS_NAMESPACE] = ('pct_40_50', Pct4050)
    _children['{%s}pct_50_60' % RAWS_NAMESPACE] = ('pct_50_60', Pct5060)
    _children['{%s}pct_60_70' % RAWS_NAMESPACE] = ('pct_60_70', Pct6070)
    _children['{%s}pct_70_80' % RAWS_NAMESPACE] = ('pct_70_80', Pct7080)
    _children['{%s}pct_80_90' % RAWS_NAMESPACE] = ('pct_80_90', Pct8090)
    _children['{%s}pct_90_100' % RAWS_NAMESPACE] = ('pct_90_100', Pct90100)
    _children['{%s}pct_unknown' % RAWS_NAMESPACE] = ('pct_unknown', PctUnknown)
    _children['{%s}time0' % RAWS_NAMESPACE] = ('time0', Time0)
    _children['{%s}time1' % RAWS_NAMESPACE] = ('time1', Time1)
    _children['{%s}time2' % RAWS_NAMESPACE] = ('time2', Time2)
    _children['{%s}time3' % RAWS_NAMESPACE] = ('time3', Time3)
    _children['{%s}time4' % RAWS_NAMESPACE] = ('time4', Time4)
    _children['{%s}time5' % RAWS_NAMESPACE] = ('time5', Time5)
    _children['{%s}time6' % RAWS_NAMESPACE] = ('time6', Time6)
    _children['{%s}time7' % RAWS_NAMESPACE] = ('time7', Time7)
    _children['{%s}time8' % RAWS_NAMESPACE] = ('time8', Time8)
    _children['{%s}sec_total' % RAWS_NAMESPACE] = ('sec_total', SecTotal)
    _children['{%s}sec_avg' % RAWS_NAMESPACE] = ('sec_avg', SecAvg)

    def __init__(self, hits=None, volume=None, path=None, updated=None, last_request=None, 
                 pct_0_10=None, pct_10_20=None, pct_20_30=None, pct_30_40 = None, pct_40_50 = None, pct_50_60 = None, 
                 pct_60_70 = None, pct_70_80 = None, pct_80_90=None, 
                 pct_90_100=None, pct_unknown=None, time0 = None, time1 = None,
                 time2=None, time3=None, time4=None, time5 = None, time6 = None, time7 = None,
                 time8=None, sec_total=None, sec_avg=None):
        rawsc.RawsParams.__init__(self, extension_elements=None, extension_attributes=None)
        self.hits = Hits(text = hits)
        self.volume = Volume(text = volume)
        self.path = Path(text = path)
        self.updated = Updated(text = updated)
        self.last_request = LastRequest(text = last_request)
        self.pct_0_10 = Pct010(text = pct_0_10)
        self.pct_10_20 = Pct1020(text = pct_10_20)
        self.pct_20_30 = Pct2030(text = pct_20_30)
        self.pct_30_40 = Pct3040(text = pct_30_40)
        self.pct_40_50 = Pct4050(text = pct_40_50)
        self.pct_50_60 = Pct5060(text = pct_50_60)
        self.pct_60_70 = Pct6070(text = pct_60_70)
        self.pct_70_80 = Pct7080(text = pct_70_80)
        self.pct_80_90 = Pct8090(text = pct_80_90)
        self.pct_90_100 = Pct90100(text = pct_90_100)
        self.pct_unknown = PctUnknown(text = pct_unknown)
        self.time0 = Time0(text = time0)
        self.time1 = Time1(text = time1)
        self.time2 = Time2(text = time2)
        self.time3 = Time3(text = time3)
        self.time4 = Time4(text = time4)
        self.time5 = Time5(text = time5)
        self.time6 = Time6(text = time6)
        self.time7 = Time7(text = time7)
        self.time8 = Time8(text = time8)
        self.sec_total = SecTotal(text = sec_total)
        self.sec_avg = SecAvg(text = sec_avg)

def TrafficParamsFromString(xml_string):
    return ratom.CreateClassFromXMLString(TrafficParams, xml_string)   


class TrafficContent(rawsc.RawsContent):
    """ Handler for the atom:content element, in a RAWS context. """
    _children = ratom.AtomBase._children.copy()
    _children['{%s}params' % (RAWS_NAMESPACE)] = ('params', TrafficParams)
    
    params = None
    
    def __init__(self, params=None):
        rawsc.RawsContent.__init__(self, extension_elements=None, extension_attributes=None)
        self.params = TrafficParams()
        if params:
            self.params = params
    
def TrafficContentFromString(xml_string):
    return ratom.CreateClassFromXMLString(TrafficContent, xml_string)   
    

class TrafficEntry(rawsc.RawsEntry):
    """ Handler for an entry that is returned by the RATS traffic resource."""
    _children = rawsc.RawsEntry._children.copy()
    _children['{%s}content' % ratom.ATOM_NAMESPACE] = ('content', TrafficContent)
    _attributes = rawsc.RawsEntry._attributes.copy()
    _attributes['{%s}kind' % RAWS_NAMESPACE] = 'kind'
    _attributes['{%s}type' % RAWS_NAMESPACE] = 'type'

    def __init__(self, author=None, category=None, atom_id=None, link=None, published=None, title=None, updated=None, content = None, extension_elements=None, extension_attributes=None):
        rawsc.RawsEntry.__init__(self, author=author, category=category, atom_id=atom_id, link=link, published=published, title=title, updated=updated,
                                extension_elements=extension_elements, extension_attributes=extension_attributes)
        self.content = TrafficContent()
        if content:
            self.content = content

def TrafficEntryFromString(xml_string):
    return ratom.CreateClassFromXMLString(TrafficEntry, xml_string)
    

class TrafficFeed(rawsc.RawsFeed, rawsc.LinkFinder):
    """ Handler for a feed that is returned by the RATS traffic resource."""
    _children = rawsc.RawsFeed._children.copy()
    _children['{%s}entry' % ratom.ATOM_NAMESPACE] = ('entry', [TrafficEntry])

def TrafficFeedFromString(xml_string):
  return ratom.CreateClassFromXMLString(TrafficFeed, xml_string)

